/*************************************************************************
 *  Copyright (C), 2017-2018, Mogoson tech. Co., Ltd.
 *  FileName: ChainEditor.cs
 *  Author: Mogoson   Version: 1.0   Date: 6/21/2017
 *  Version Description:
 *    Internal develop version,mainly to achieve its function.
 *  File Description:
 *    Ignore.
 *  Class List:
 *    <ID>           <name>             <description>
 *     1.         ChainEditor             Ignore.
 *  Function List:
 *    <class ID>     <name>             <description>
 *     1.
 *  History:
 *    <ID>    <author>      <time>      <version>      <description>
 *     1.     Mogoson     6/21/2017       1.0        Build this file.
 *************************************************************************/

namespace Developer.MechanicalDrive
{
    using UnityEditor;
    using UnityEditor.SceneManagement;
    using UnityEngine;

    [CustomEditor(typeof(Chain), true)]
    [CanEditMultipleObjects]
    public class ChainEditor : MeEditor
    {
        #region Property and Field
        protected Chain script { get { return target as Chain; } }
        #endregion

        #region Protected Method
        protected void OnEnable()
        {
            if (script.anchorRoot)
                script.anchorRoot.localPosition = Vector3.zero;
            if (script.nodeRoot)
                script.nodeRoot.localPosition = Vector3.zero;
        }

        protected void DeleteNodes()
        {
            while (script.nodeRoot.childCount > 0)
            {
                DestroyImmediate(script.nodeRoot.GetChild(0).gameObject);
            }
        }
        #endregion

        #region Public Method
        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Create"))
            {
                DeleteNodes();
                script.InitialiseNodes();
                EditorSceneManager.MarkAllScenesDirty();
            }
            if (GUILayout.Button("Delete"))
            {
                DeleteNodes();
                EditorSceneManager.MarkAllScenesDirty();
            }
            GUILayout.EndHorizontal();
        }
        #endregion
    }//class_end
}//namespace_end